package cn.com.controller;

import cn.com.pojo.Merchant;
import cn.com.pojo.User;
import cn.com.service.MerchantService;
import cn.com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService service;


    @GetMapping("/submit")
    public String Submit(User user) {
        user.setPermission("用户");
        user.setAccount("0");
        service.insertUser(user);
        return "login";
    }
    @Autowired
    MerchantService merchantService;
    @PostMapping ("/login")
    public String login(User user, HttpSession session, Model model) {
        User user1 = service.queryCount(user.getUname());
        if (user1 == null) {
            return "submit";
        }
        if (user.getUname().equals(user1.getUname()) && user.getPwd().equals(user1.getPwd())) {
            switch (user1.getPermission()) {
                case "管理员":
                        session.setAttribute("USER", user1);
                        return "admin";

                case "用户":
                        session.setAttribute("USER", user1);
                        return "index";

                case "商家":
                    Merchant shop = merchantService.showOne(user.getUname());
                    if (shop.getFlag().equals("0")) {return "baned";}else{
                        session.setAttribute("SHOP", shop);
                        session.setAttribute("USER1", user1);
                        return "shop";
                    }

                default:return "login";
            }
        }
        else {
            model.addAttribute("warn", "账号或密码错误");
            return "login";
        }

    }
    @PostMapping("/toUser")
    public String toUser(String userName,Model model){
        Merchant merchant=merchantService.showOne(userName);
        model.addAttribute("shop",merchant);
        return "user";
    }
}
